// Created from GroupGenerationTemplate
// For Decisions HL7 Engine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using DecisionsFramework.Design.Properties;
using Decisions.HL7.DataTypes;
using ST = System.String;
using SI = System.String;
using IS = System.String;
using ID = System.String;
using SNM = System.String;

namespace Decisions.HL7.Message
{
    [DataContract]
	public class ORDER_OPL_O37 : AbstractGroup
    {
        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("11 NK1",11)]
  	    [HL7Field(HL7Version.v26, 11, HL7Repeat.True, HL7Required.True,"NK1")]
	    public NK1[] NK1 {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("12 PATIENT",12)]
  	    [HL7Field(HL7Version.v26, 12, HL7Repeat.False, HL7Required.False,"PATIENT_OPL_O37")]
	    public PATIENT_OPL_O37 PATIENT {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("23 SPECIMEN",23)]
  	    [HL7Field(HL7Version.v26, 23, HL7Repeat.True, HL7Required.True,"SPECIMEN_OPL_O37")]
	    public SPECIMEN_OPL_O37[] SPECIMEN {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("43 PRIOR_RESULT",43)]
  	    [HL7Field(HL7Version.v26, 43, HL7Repeat.False, HL7Required.False,"PRIOR_RESULT_OPL_O37")]
	    public PRIOR_RESULT_OPL_O37 PRIOR_RESULT {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("65 FT1",65)]
  	    [HL7Field(HL7Version.v26, 65, HL7Repeat.True, HL7Required.False,"FT1")]
	    public FT1[] FT1 {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("66 CTI",66)]
  	    [HL7Field(HL7Version.v26, 66, HL7Repeat.True, HL7Required.False,"CTI")]
	    public CTI[] CTI {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("67 BLG",67)]
  	    [HL7Field(HL7Version.v26, 67, HL7Repeat.False, HL7Required.False,"BLG")]
	    public BLG BLG {get;set;}



		#region Parser Helpers and Conrete Impls

        internal override void ConsumeTextSegmentsForGroup(Stack<string> availableData, HL7ParserOptions options) {

			NK1 = PopSegmentParseAndCheckRequiredArray<NK1>(availableData, options, "NK1");
			PATIENT = ProcessGroup<PATIENT_OPL_O37>(availableData, options, "PATIENT");
			SPECIMEN = ProcessGroupArray<SPECIMEN_OPL_O37>(availableData, options, "SPECIMEN");
			PRIOR_RESULT = ProcessGroup<PRIOR_RESULT_OPL_O37>(availableData, options, "PRIOR_RESULT");
			FT1 = PopSegmentParseAndCheckRequiredArray<FT1>(availableData, options, "FT1");
			CTI = PopSegmentParseAndCheckRequiredArray<CTI>(availableData, options, "CTI");
			BLG = PopSegmentParseAndCheckRequired<BLG>(availableData, options, "BLG");
        }

		internal override bool CanConsume(string availableData, HL7ParserOptions options)
        {
			bool isNK1 = availableData.StartsWith("NK1", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("NK1", options.Version) || isNK1) {
				return isNK1;
			}
			PATIENT_OPL_O37 testPATIENT_OPL_O37 = new PATIENT_OPL_O37();
			bool grpCCPATIENT_OPL_O37 = (testPATIENT_OPL_O37.CanConsume(availableData, options));
			if (IsRequired("PATIENT", options.Version) || grpCCPATIENT_OPL_O37) {
				return grpCCPATIENT_OPL_O37;
			}
			SPECIMEN_OPL_O37 testSPECIMEN_OPL_O37 = new SPECIMEN_OPL_O37();
			bool grpCCSPECIMEN_OPL_O37 = (testSPECIMEN_OPL_O37.CanConsume(availableData, options));
			if (IsRequired("SPECIMEN", options.Version) || grpCCSPECIMEN_OPL_O37) {
				return grpCCSPECIMEN_OPL_O37;
			}
			PRIOR_RESULT_OPL_O37 testPRIOR_RESULT_OPL_O37 = new PRIOR_RESULT_OPL_O37();
			bool grpCCPRIOR_RESULT_OPL_O37 = (testPRIOR_RESULT_OPL_O37.CanConsume(availableData, options));
			if (IsRequired("PRIOR_RESULT", options.Version) || grpCCPRIOR_RESULT_OPL_O37) {
				return grpCCPRIOR_RESULT_OPL_O37;
			}
			bool isFT1 = availableData.StartsWith("FT1", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("FT1", options.Version) || isFT1) {
				return isFT1;
			}
			bool isCTI = availableData.StartsWith("CTI", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("CTI", options.Version) || isCTI) {
				return isCTI;
			}
			bool isBLG = availableData.StartsWith("BLG", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("BLG", options.Version) || isBLG) {
				return isBLG;
			}
			return false;
        }

        private static Dictionary<string, bool> SEGMENT_VERSION_REQUIRED_MAP = new Dictionary<string, bool>();

		protected override Dictionary<string, bool> MAP_OF_SEGMENT_TO_REQUIRED
        {
            get
			{
				return SEGMENT_VERSION_REQUIRED_MAP;
			}
        }

		public override string HL7ENTITY_TYPE_NAME
        {
            get {
				return "ORDER";
			}
        }

        static ORDER_OPL_O37() {

             SetupStaticPropertyMap(SEGMENT_VERSION_REQUIRED_MAP, typeof(ORDER_OPL_O37));

        }

		#endregion

		public override string GetER7(HL7ParserOptions options)
		{
			List<string> segmentData = new List<string>();
		 if (NK1 != null && NK1.Length > 0) {
			for (int i = 0; i < NK1.Length; i++) {
				string segmentValue = NK1[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
		 if (PATIENT != null) {
			string segmentValue = PATIENT.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (SPECIMEN != null && SPECIMEN.Length > 0) {
			for (int i = 0; i < SPECIMEN.Length; i++) {
				string segmentValue = SPECIMEN[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
		 if (PRIOR_RESULT != null) {
			string segmentValue = PRIOR_RESULT.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (FT1 != null && FT1.Length > 0) {
			for (int i = 0; i < FT1.Length; i++) {
				string segmentValue = FT1[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
		 if (CTI != null && CTI.Length > 0) {
			for (int i = 0; i < CTI.Length; i++) {
				string segmentValue = CTI[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
		 if (BLG != null) {
			string segmentValue = BLG.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
           return string.Join(HL7ParserConstants.SEGMENT_SEPARATOR, segmentData.ToArray());
        
        }
    }
}

